package leetcode;

import java.nio.charset.Charset;
import java.util.Stack;

/**
 * Given a string, you need to reverse the order of characters in each word within a sentence
 * while still preserving whitespace and initial word order.
 * Example 1:
 * Input: "Let's take LeetCode contest"
 * Output: "s'teL ekat edoCteeL tsetnoc"
 */
public class ReverseWordsInAStringIII {
    public static void main(String[] args) {
        System.out.println(reverseWords("Let's take LeetCode contest"));
    }

    public static String reverseWords(String s) {
        String res = "";
        String[] strings = s.split(" ");
        for (String str : strings
             ) {
            char[] chars = str.toCharArray();
            int start = 0,end = chars.length - 1;
            char temp;
            while (start < end){
                temp = chars[start];
                chars[start] = chars[end];
                chars[end] = temp;
                start++;
                end--;
            }
            res += new String(chars) + " ";
        }
        res = res.substring(0, res.length() - 1);
        return res;
    }
}
